package view;

import data.Data_Book;
import data.Data_Canvas;
import tree.Root;
import cmd.Root_Cmd;
import tree.Leaf;
import haxe.macro.Type.ClassType;
import hxd.Pixels.Flags;
import tree.LeafMsg;
import h2d.domkit.Style;
import tree.LeafKind;
import hxd.Window;
import h2d.Flow;
import tree.Tress;

class ViewTress<T:Flow> extends Tress{
    private var m_center : Flow;
    private var m_view : T;
    private var m_isInit : Bool;
    private var m_style : Style;
    

    
    
    public function new(t:Class<T>, ?view : T) {
        super();
        Init(t, view);
    }

    private function Init(t:Class<T>, ?view:T) {
        m_style = new Style();
        m_style.load(Res.load(GetCSSPath()));
        m_view = view;
        if(m_view == null){
            m_center = new Flow(Main.instance.UIRoot);
            m_center.horizontalAlign = m_center.verticalAlign = Middle;
            m_view = Type.createInstance(t, [m_center]);
            m_view.maxHeight = m_view.minHeight = Main.Height;
            m_view.maxWidth = m_view.minWidth = Main.Width;
        }
        //m_view.dom.applyStyle(m_style, true);
		m_style.addObject(m_view);
    }


    private function GetCSSPath() : String{
        var s = Std.string(this);
        var res = s.split('.');
        var n = res[res.length - 1].toLowerCase();
        n = "css/" + n + ".css";
        return n;
    }


	public override function OnEnter(last:LeafKind) {
        super.OnEnter(last);
        
	}

    public override function OnUpdate(msg:LeafMsg) {
        super.OnUpdate(msg);
        if(m_style != null){
            m_style.sync();
        }
    }

}